/*
 * Created by SharpDevelop.
 * User: Jobe
 * Date: 16.09.2006
 * Time: 15:10
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Win32;
using System.Diagnostics;

namespace LauncherPlus
{

	/// <summary>Configuration</summary>
	public class cConfig{
		
		private NameValueCollection Settings = new NameValueCollection();
		private cGame Game = null;

		/// <summary>Game settings (../WTF/Config.wtf)</summary>
		public cConfig(cGame Game){
			this.Game = Game;
			Load();
		}
		
		/// <summary>Load game settings</summary>
		public void Load(){
			// parse config.wtf into hashtable
			Settings.Clear();
			Regex mask = new Regex("^SET ([a-zA-Z]*) \"([^\"]*)\"$");
			FileInfo ConfigFile = new FileInfo(Game.InstallPath.FullName+"/WTF/Config.wtf");
			if(!ConfigFile.Exists)throw new Exception("Couldn't find Config.wtf");
			StreamReader configStream = ConfigFile.OpenText();
			while(!configStream.EndOfStream){
				Match m = mask.Match(configStream.ReadLine());
				if(m.Success)Settings.Add(m.Groups[1].Captures[0].Value,m.Groups[2].Captures[0].Value);
			}
			configStream.Close();
		}
		
		/// <summary>Save game settings</summary>
		public void Save(){
			FileInfo ConfigFile = new FileInfo(Game.InstallPath.FullName+"/WTF/Config.wtf");
			if(!ConfigFile.Exists)throw new Exception("Couldn't find Config.wtf");
			TextWriter twConfig = new StreamWriter(ConfigFile.FullName);
			foreach( string key in Settings.AllKeys ){
				twConfig.WriteLine("SET "+key+" \""+Settings.Get(key)+"\"");
			}
			twConfig.Close();
		}
		
		/// <summary>Get game settings value</summary>
		/// <param name="key">Name of setting to return</param>
		public string Get(string key){
			return Settings.Get(key);
		}
		
		/// <summary>Set game settings value</summary>
		/// <param name="key">Name of setting to set</param>
		/// <param name="val">Value to set</param>
		public void Set(string key, string val ){
			Settings.Set(key,val);
		}
		
		/// <summary>Get a list of all available settings keys</summary>
		/// <returns>List of all keys</returns>
		public string[] AllKeys{
			get{return Settings.AllKeys;}
		}
		
	}
	
	/// <summary>Languages</summary>
	public class cLocale{
		
		private cGame m_Game = null;
		private string m_Name = null;
		private string m_Code = null;
		private string m_URL = null;
		private bool m_Installed = false;
		public DirectoryInfo Folder = null;
		
		/// <summary>Returns true if language is installed</summary>
		public bool Installed{
			get{return m_Installed;}
		}
		
		/// <summary>Full name of language</summary>
		public string Name{
			get{return m_Name;}
		}
		
		/// <summary>Short code for language</summary>
		public string Code{
			get{return m_Code;}
		}
		
		/// <summary>URL to localized blizz-news</summary>
		public string URL{
			get{return m_URL;}
		}
		
		public cLocale(cGame Game, string Name, string Code, string URL){
			m_Game = Game;
			m_Name = Name;
			m_Code = Code;
			m_URL = URL;
			Folder = new DirectoryInfo(Game.InstallPath.FullName+"/Data/"+Code);
			if(Folder.Exists){
				// language is installed if mpg-file exists
				FileInfo mpq = new FileInfo(Folder.FullName+"/locale-"+Code+".MPQ");
				m_Installed = mpq.Exists;
			}
		}
		
	}
	
	/// <summary>WoW Game Client</summary>
	public class cGame{
		
		public FileInfo GamePath = null;
		public DirectoryInfo InstallPath = null;
		public DirectoryInfo Addons = null;
		public cConfig Config = null;
		public cLocale[] Locale = null;
		
		private FileVersionInfo WOWFileVersionInfo = null;
		
		public cGame(){
			// get wow-installation from registry
			RegistryKey lm = Registry.LocalMachine;
	        RegistryKey rk = lm.OpenSubKey(@"SOFTWARE\Blizzard Entertainment\World of Warcraft");
	        this.InstallPath = new DirectoryInfo(rk.GetValue("InstallPath").ToString());
			this.WOWFileVersionInfo = FileVersionInfo.GetVersionInfo( this.InstallPath.FullName+"/WoW.exe" );
	        this.GamePath = new FileInfo(rk.GetValue("GamePath").ToString());
	        this.Addons = new DirectoryInfo(this.InstallPath.FullName+@"\Interface\AddOns");
	        this.Config = new cConfig(this);
	        this.Locale = new cLocale[8];
	        // wow languages:
			//\Data\deDE\locale-deDE.MPQ
			//\Data\esES\locale-esES.MPQ
			//\Data\enGB\locale-enGB.MPQ
			//\Data\enUS\locale-enUS.MPQ
			//\Data\frFR\locale-frFR.MPQ
			//\Data\koKR\locale-koKR.MPQ
			//\Data\zhCN\locale-zhCN.MPQ
			//\Data\zhTW\locale-zhTW.MPQ
			// launcher urls:
			//http://launcher.worldofwarcraft.com/
			//http://launcher.wow-europe.com/de/
			//http://launcher.wow-europe.com/en/
			//http://launcher.wow-europe.com/es/
			//http://launcher.wow-europe.com/fr/
			//http://launcher.worldofwarcraft.co.kr/
			//http://launcher.worldofwarcraft.com/
			//http://download.wowtaiwan.com.tw/launcher/
			this.Locale[0] = new cLocale(this,"German",			"deDE", "http://launcher.wow-europe.com/de/");
	        this.Locale[1] = new cLocale(this,"Spanish",		"esES", "http://launcher.wow-europe.com/es/");
	        this.Locale[2] = new cLocale(this,"English (EU)",	"enGB", "http://launcher.wow-europe.com/en/");
	        this.Locale[3] = new cLocale(this,"English",		"enUS", "http://launcher.worldofwarcraft.com/");
	        this.Locale[4] = new cLocale(this,"French",			"frFR", "http://launcher.wow-europe.com/fr/");
	        this.Locale[5] = new cLocale(this,"Korean",			"koKR", "http://launcher.worldofwarcraft.co.kr/");
	        this.Locale[6] = new cLocale(this,"Chinese",		"zhCN", "http://launcher.worldofwarcraft.com/");
	        this.Locale[7] = new cLocale(this,"Taiwan",			"zhTW", "http://download.wowtaiwan.com.tw/launcher/");
		}

		/// <summary>Returns index of active language</summary>
		public int ActiveLanguage{
			get{
		        string code = Config.Get("locale");
		        int index = -1;
		        for( int i = 0 ; i < Locale.Length ; i++ )if(Locale[i].Code.Equals(code))index = i;
		        return index;
			}
		}
		
		public string FileVersion{
			get{return WOWFileVersionInfo.FileVersion.Replace(",",".").Replace(" ","");}
		}
		
		public string ProductName{
			get{return WOWFileVersionInfo.ProductName;}
		}
	}
}
